package com.chenyue.cm.api.live;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.domain.JdLiveReport;
import com.chenyue.cm.common.domain.JdReportType;
import com.chenyue.cm.common.service.JdLiveReportService;
import com.chenyue.cm.common.service.JdReportTypeService;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.live.domain.JdLive;
import com.chenyue.cm.live.service.JdLiveService;
import com.chenyue.cm.match.domain.FormatLiveRecord;
import com.chenyue.cm.match.service.FormatLiveRecordService;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.user.domain.JdUserCollection;
import com.chenyue.cm.user.service.JdUserCollectionService;
import com.chenyue.cm.utils.DateUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author libingxing
 */
@Api(tags = "直播记录")
@ApiSort(3)
@RestController
@RequestMapping("/api/liveRecord")
public class ApiLiveRecordController {

    private final FormatLiveRecordService formatLiveRecordService;

    private final JdLiveService liveService;

    private final JdUserCollectionService userCollectionService;

    private final JdLiveReportService liveReportService;

    private final JdReportTypeService reportTypeService;

    public ApiLiveRecordController(FormatLiveRecordService formatLiveRecordService, JdLiveService liveService, JdUserCollectionService userCollectionService, JdLiveReportService liveReportService, JdReportTypeService reportTypeService) {
        this.formatLiveRecordService = formatLiveRecordService;
        this.liveService = liveService;
        this.userCollectionService = userCollectionService;
        this.liveReportService = liveReportService;
        this.reportTypeService = reportTypeService;
    }

    @Login
    @GetMapping("/getRecord")
    @ApiOperation("我的直播记录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "page", dataType = "integer", required = true),
            @ApiImplicitParam(value = "页码", name = "rows", dataType = "integer", required = true),
    })
    public Success<Page<List<FormatLiveRecord>>> getRecord(
            @ApiIgnore @LoginUser JdAppUser user,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows
    ) {
        JdLive live = liveService.getOne(new QueryWrapper<JdLive>().lambda().eq(JdLive::getUserId, user.getId()).last("limit 1"));
        if (live == null) {
            return Success.error("您还不是主播");
        }
        LambdaQueryWrapper<FormatLiveRecord> queryWrapper = new QueryWrapper<FormatLiveRecord>()
                .lambda()
                .eq(FormatLiveRecord::getLiveId, live.getId())
                .isNotNull(FormatLiveRecord::getEndTime)
                .orderByDesc(FormatLiveRecord::getStartTime);
        int total = formatLiveRecordService.count(queryWrapper);
        Page<List<FormatLiveRecord>> result = new Page<>(page, rows, total);
        queryWrapper.last(result.convertPage());
        List<FormatLiveRecord> list = formatLiveRecordService.list(queryWrapper);
        result.setContent(list);
        return Success.success(result);
    }

    @Login
    @PostMapping("/collectLive")
    @ApiOperation("收藏回放（userId不传）")
    public Success<String> collectLive(
            @LoginUser @ApiIgnore JdAppUser user,
            @ApiParam JdUserCollection collection
    ) {
        collection.setUserId(Integer.valueOf(user.getId()));
        final JdUserCollection one = userCollectionService.lambdaQuery()
                .eq(JdUserCollection::getColId, collection.getColId())
                .eq(JdUserCollection::getColType, collection.getColType())
                .eq(JdUserCollection::getUserId, user.getId())
                .one();
        if (one == null) {
            userCollectionService.save(collection);
        }
        return Success.success();
    }

    @Login
    @PostMapping("/deleteCollect")
    @ApiOperation("取消收藏（userId不传）")
    public Success<String> deleteCollect(
            @LoginUser @ApiIgnore JdAppUser user,
            @ApiParam JdUserCollection collection
    ) {
        collection.setUserId(Integer.valueOf(user.getId()));
        userCollectionService.cancelCollect(collection);
        return Success.success();
    }

    @PostMapping("/reportType")
    @ApiOperation("举报类型")
    public Success<List<JdReportType>> reportType() {
        return Success.success(reportTypeService.list());
    }

    @Login
    @PostMapping("/reportLive")
    @ApiOperation("直播间举报")
    public Success<String> reportLive(
            @LoginUser @ApiIgnore JdAppUser user,
            @ApiParam JdLiveReport report
    ) {
        report.setCt(DateUtils.getToday());
        report.setUserId(Integer.valueOf(user.getId()));
        liveReportService.save(report);
        return Success.success();
    }

}
